import { _decorator, Component, log, Node, Quat, RigidBody, RigidBody2D, v2, Vec2, Vec3 } from 'cc';
import DataManager from './Base/DataManager';
const { ccclass, property } = _decorator;

@ccclass('Arrow')
export class Arrow extends Component {

    private rigidBody: RigidBody2D

    start() {
        this.rigidBody = this.node.getComponent(RigidBody2D);
    }

    update(deltaTime: number) {
        // const rigidBody = this.node.getComponent(RigidBody2D);
        // const angle = rigidBody.linearVelocity.signAngle(v2(1,0));
        // this.node.angle = -(angle * 180 / Math.PI);

        //获取钢体速度（向量计算角度）
        const v = this.rigidBody.linearVelocity

        //把速度转换成弧度，y + x * 固定值
        // float angle = Mathf.Atan2(v.y, v.x) * Mathf.Rad2Deg - 90; //因为形状是打直的，-90度刚好放平打横
        const angle = Math.atan2(v.y, v.x) * (180 / Math.PI) - 90;
        //弧度转化成角度赋值给箭本身
        this.node.angle = angle

        let weizhi = {
            x: DataManager.Instance.fixedPoint(this.node.position.x),
            y: DataManager.Instance.fixedPoint(this.node.position.y),
            angle: DataManager.Instance.fixedPoint(angle)
        }

        DataManager.Instance.myState.push(weizhi)
        
        log('当前位置：',this.node.position,'，角度：', angle)

        if(Math.abs(this.node.position.x) > 600){
            this.node.destroy()
        }

        // 计算夹角
        // const angle = this.rigidBody.linearVelocity.clone().signAngle(v2(1, 0));
        // this.rigidBody.node.rotation = angle * 180 / Math.PI;
    }
}


